import {UserRegisterMessage} from "./UserRegisterMessage";
import {Utils} from "../services/Utils";

export class ValidateUserInput {
    static checkUsername(username:string):UserRegisterMessage {
        let resultMessage:UserRegisterMessage = new UserRegisterMessage();
        if (Utils.getRealByteSize(username) > 12) {
            resultMessage.flag = false;
            resultMessage.message = "最多六个汉字或最多12个英文字母";
        } else if (ValidateUserInput.checkInvalidSign(username)) {
            resultMessage.flag = false;
            resultMessage.message = "用户名中存在空格~！@#$%^&*()<>?{}[]等无效字符";
        } else {
            resultMessage.flag = true;
            resultMessage.message = "";
        }

        return resultMessage;
    }

    static checkPassword(password:string):UserRegisterMessage {
        let resultMessage = new UserRegisterMessage();

        if (Utils.getRealByteSize(password) < 6 || Utils.getRealByteSize(password) > 16) {
            resultMessage.flag = false;
            resultMessage.message = "位数少于6位或多于16位";
        } else {
            resultMessage.flag = true;
            resultMessage.message = "";
        }

        return resultMessage;
    }

    static checkConfirm(confirm:string, password:string):UserRegisterMessage {
        let resultMessage = new UserRegisterMessage();
        let flag:boolean = confirm === password;
        resultMessage.flag = flag;
        resultMessage.message = flag ? "" : "两次密码不一致，请确认";
        return resultMessage;
    }

    static checkPhone(phone:string):UserRegisterMessage {
        let resultMessage = new UserRegisterMessage();

        resultMessage.flag = /^1(3|4|5|8|7)[0-9]\d{8}$/.test(phone);
        resultMessage.message = resultMessage.flag ? "" : "手机号格式不正确，请确认";

        return resultMessage;
    }

    static checkPhoneVerificationCode(phoneVerificationCode:string):UserRegisterMessage {
        let resultMessage:UserRegisterMessage = new UserRegisterMessage();

        resultMessage.flag = phoneVerificationCode === "8888";
        resultMessage.message = resultMessage.flag ? "" : "手机验证码错误，请确认";

        return resultMessage;
    }

    static checkVerificationCode(verificationCode:string, verificationResult:string):UserRegisterMessage {
        let resultMessage:UserRegisterMessage = new UserRegisterMessage();

        resultMessage.flag = verificationCode.toLowerCase() === verificationResult.toLowerCase();
        resultMessage.message = resultMessage.flag ? "" : "验证码错误，请确认";

        return resultMessage;
    }

    static checkInvalidSign(target:string):boolean {
        let signs:Array<string> = Utils.invalidSign();
        for (let i = 0; i < signs.length; i++) {
            if (target.indexOf(signs[i]) != -1) {
                return true;
            }
        }

        return false;
    }

}